<?php
include("include/config.inc.php");

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$logDataArray = 0;
	$incDtlArray  = 0;
	$incomingId = isset($_REQUEST['incomingId']) ? $_REQUEST['incomingId'] : 0;
	if(isset($_POST['pmSubmitBtn']))
	{
		$pmDate           = $_POST['pmDateYear']."-".$_POST['pmDateMonth']."-".$_POST['pmDateDay'];
		$startTime        = date('Y-m-d')." ".$_POST['startTimeHour'].":".$_POST['startTimeMinute'].":00";
		$soakingStartTime = date('Y-m-d')." ".$_POST['soakStartTimeHour'].":".$_POST['soakStartTimeMinute'].":00";
		$endTime          = date('Y-m-d')." ".$_POST['endTimeHour'].":".$_POST['endTimeMinute'].":00";
		$extraTempAttain  = date('Y-m-d')." ".$_POST['extraTempAttainHour'].":".$_POST['extraTempAttainMinute'].":00";
		$extraSoaking     = date('Y-m-d')." ".$_POST['extraSoakingHour'].":".$_POST['extraSoakingMinute'].":00";
		$extraOut         = date('Y-m-d')." ".$_POST['extraOutHour'].":".$_POST['extraOutMinute'].":00";
		$column1          = (isset($_POST['column1']));
		$logSheetId       = (isset($_POST['logSheetId'])) ? $_POST['logSheetId'] :0 ;
		
		if(isset($_POST['putExtraData']) && $_POST['putExtraData'] > 0)
		{
		  $extraDataQuery = "putExtraData,extraTempAttain,extraTempAttainTemperature,extraSoaking,extraSoakingTemperature,extraOut,extraOutTemperature,";
		  $extraDataValue = " ".$_POST['putExtraData'].",
		                      '".$extraTempAttain."','".$_POST['extraTempAttainTemperature']."',
		                      '".$extraSoaking."','".$_POST['extraSoakingTemperature']."',
		                      '".$extraOut."','".$_POST['extraOutTemperature']."', ";
    }
    else
    {
    	$extraDataQuery = NULL;
    	$extraDataValue = NULL;
    }
		
		$insPMMaster = "INSERT INTO pmmaster(pmDate, incomingId, newPartyName, planningId, batchNo,logSheetId, processId, newProcessName,
		                                     quantity, weight, materialId, startTime, startTemperature, soakingStartTime, 
		                                     soakingStartTemperature, soakingHour, soakingHourTemperature, endTime, endTemperature, 
		                                     qMId, quenchingTime, tempBforQue, tempAfterQue,".$extraDataQuery." notes)
                    VALUES('".$pmDate."',".$_POST['incomingId'].",'".$_POST['newPartyName']."',".$_POST['planningId'].",'".$_POST['batchNo']."',
                           ".$_POST['logSheetId'].",".$_POST['processId'].",'".$_POST['newProcessName']."',".$_POST['quantity'].",
                           ".$_POST['weight'].",".$_POST['materialId'].",'".$startTime."','".$_POST['startTemperature']."',
                           '".$soakingStartTime."','".$_POST['soakingStartTemperature']."','".$_POST['soakingHour']."',
                           '".$_POST['soakingHourTemperature']."','".$endTime."','".$_POST['endTemperature']."',".$_POST['qMId'].",
                           ".$_POST['quenchingTime'].",".$_POST['tempBforQue'].",".$_POST['tempAfterQue'].",".$extraDataValue." '".nl2br(addslashes($_POST['notes']))."')";
		$insPMMasterRes = mysql_query($insPMMaster);
		$pmMasterId = mysql_insert_id();
		if($insPMMasterRes)
		{
			if(isset($_POST['IncomingDetailId']))
			{
				foreach($_POST['IncomingDetailId'] AS $keyId => $values)
				{
					if($_POST['columns1'][$keyId] > 0)
				  $insPMDetailone = "INSERT INTO pmdetailone(pmMasterId,IncomingDetailId, columns1, columns2, columns3, columns4, columns5, columns6)
				                     VALUES(".$pmMasterId.",".$values.",'".$_POST['columns1'][$keyId]."','".$_POST['columns2'][$keyId]."',
				                            '".$_POST['columns3'][$keyId]."','".$_POST['columns4'][$keyId]."','".$_POST['columns5'][$keyId]."',
				                            '".$_POST['columns6'][$keyId]."')";
				  $insPMDetailoneRes = mysql_query($insPMDetailone);
				}
			}
		}
		if($insPMMasterRes)
		{
			$j = 0;
      for($i=0; $i < count($_POST['detailTime']); $i++)
			{
				if(isset($_POST['detailTime'][$i]['Time_Hour']))
				{
					$detailTime = "";
					$detailTime = date('Y-m-d')." ".$_POST['detailTime'][$i]['Time_Hour'];
				}
				if(isset($_POST['detailTime'][$i]['Time_Minute']))
				{
					$detailTime .= ":".$_POST['detailTime'][$i]['Time_Minute'].":00";
					$insPMDetailtwo = "INSERT INTO pmdetailtwo(pmMasterId, detailTime, detailTemperature)
					                   VALUES (".$pmMasterId.",'".$detailTime."','".$_POST['detailTemperature'][$j]."')";
          $insPMDetailtwoRes = mysql_query($insPMDetailtwo);
					$j++;
				}
		  }
		}
		if($insPMMasterRes)
		{
			$updateLGSheet = "UPDATE logsheet
			                     SET pmrDone = 'Y',
			                         pmMasterId = ".$pmMasterId."
			                   WHERE logSheetId = ".$_POST['logSheetId'];
      $updateLGSheetRes = mysql_query($updateLGSheet);
	  }
	  
		if($incomingId > 0)
		{
			$updatedata = "UPDATE incoming  SET 
			                                        column1 = '".$_POST['column1']."',
			                                        column2 = '".$_POST['column2']."',
			                                        column3 = '".$_POST['column3']."',
			                                        column4 = '".$_POST['column4']."',
			                                        column5 = '".$_POST['column5']."',
			                                        column6 = '".$_POST['column6']."'
			                   WHERE incomingId = ".$_REQUEST['incomingId'];
			$updatedataRes = mysql_query($updatedata);
			if(!$updatedataRes)
			{
				echo "UPDATE FAIL";
			}
			else
			{
				echo "Update Sucessfully";
			}
		}	  
	  
	  header('Location:materialReport.php');
	}
	
	if(isset($_GET['logSheetId']) && $_GET['logSheetId'] > 0)
	{
		$selectData = "SELECT logSheetId,logsheet.incomingId,logsheet.processId,logsheet.planningId,processNo,
		                      logsheetNo,DATE_FORMAT(logsheetDate,'%d-%m-%Y') AS logsheetDate,startTime,
		                      startTemperature,soakingStartTime,endTime,endTemperature,qMId,quenchingTime,tempBforQue,
		                      tempAfterQue,incoming.incomingId,incoming.challan AS incChallan,materialId,column1,
		                      column2,column3,column4,column5,column6,planning.quantity,planning.weight,temp1,
		                      soaking1,furnace1.furnaceName AS fName1,temp2,soaking2,furnace2.furnaceName AS fName2,
		                      temp3,soaking3,furnace3.furnaceName AS fName3,partyName,processName
		                 FROM logsheet
		                 JOIN incoming ON logsheet.incomingId = incoming.incomingId
		                 JOIN planning ON logsheet.planningId = planning.planningId
		                 JOIN party ON incoming.partyId = party.partyId
		                 JOIN process ON logsheet.processId = process.processId
                     LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                     LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                     LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
		                WHERE logSheetId = ".$_GET['logSheetId'];
    $selectDataRes = mysql_query($selectData);
    $logDataArray = array();
    if($logRow = mysql_fetch_array($selectDataRes))
    {
    	$logDataArray['logSheetId']       = $logRow['logSheetId'];
      $logDataArray['incomingId']       = $logRow['incomingId'];
    	$logDataArray['planningId']       = $logRow['planningId'];
    	$logDataArray['processId']        = $logRow['processId'];
    	$logDataArray['processNo']        = $logRow['processNo'];
    	$logDataArray['logsheetNo']       = $logRow['logsheetNo'];
    	$logDataArray['logsheetDate']     = $logRow['logsheetDate'];
    	$logDataArray['startTime']        = $logRow['startTime'];
    	$logDataArray['startTemperature'] = $logRow['startTemperature'];
    	$logDataArray['soakingStartTime'] = $logRow['soakingStartTime'];
    	$logDataArray['endTime']          = $logRow['endTime'];
    	$logDataArray['endTemperature']   = $logRow['endTemperature'];
    	$logDataArray['qMId']             = $logRow['qMId'];
    	$logDataArray['quenchingTime']    = $logRow['quenchingTime'];
    	$logDataArray['tempBforQue']      = $logRow['tempBforQue'];
    	$logDataArray['tempAfterQue']     = $logRow['tempAfterQue'];
    	$logDataArray['incChallan']       = $logRow['incChallan'];
    	$logDataArray['materialId']       = $logRow['materialId'];
    	$logDataArray['column1']          = $logRow['column1'];
    	$logDataArray['column2']          = $logRow['column2'];
    	$logDataArray['column3']          = $logRow['column3'];
    	$logDataArray['column4']          = $logRow['column4'];
    	$logDataArray['column5']          = $logRow['column5'];
    	$logDataArray['column6']          = $logRow['column6'];
    	$logDataArray['quantity']         = $logRow['quantity'];
    	$logDataArray['weight']           = $logRow['weight'];
    	
    	if($logRow['processNo'] == 1)
    	{
    	  $logDataArray['temp']           = $logRow['temp1'];
    	  $logDataArray['soaking']        = $logRow['soaking1'];
    	  $logDataArray['furnace']        = $logRow['fName1'];
    	}
    	else if($logRow['processNo'] == 2)
    	{
    	  $logDataArray['temp']           = $logRow['temp2'];
    	  $logDataArray['soaking']        = $logRow['soaking2'];
    	  $logDataArray['furnace']        = $logRow['fName2'];
    	}
    	else if($logRow['processNo'] == 3)
    	{
    	  $logDataArray['temp']           = $logRow['temp3'];
    	  $logDataArray['soaking']        = $logRow['soaking3'];
    	  $logDataArray['furnace']        = $logRow['fName3'];
    	}
    	$logDataArray['partyName']        = $logRow['partyName'];
    	$logDataArray['processName']      = $logRow['processName'];
    	
    	$selIncDtl = "SELECT IncomingDetailId,columns1,columns2,columns3,columns4,columns5,columns6
    	                FROM incomingdetail
    	               WHERE IncomingId = ".$logRow['incomingId'];
      $selIncDtlRes = mysql_query($selIncDtl);
      $incDtlArray  = array();
      $i = 0;
      while($dtlRow = mysql_fetch_array($selIncDtlRes))
      {
      	$incDtlArray[$i]['IncomingDetailId'] = $dtlRow['IncomingDetailId'];
      	$incDtlArray[$i]['columns1'] = $dtlRow['columns1'];
      	$incDtlArray[$i]['columns2'] = $dtlRow['columns2'];
      	$incDtlArray[$i]['columns3'] = $dtlRow['columns3'];
      	$incDtlArray[$i]['columns4'] = $dtlRow['columns4'];
      	$incDtlArray[$i]['columns5'] = $dtlRow['columns5'];
      	$incDtlArray[$i]['columns6'] = $dtlRow['columns6'];
      	$i++;
      }
    }
	}
	//---------------------material---------------------------
  $m = 0;                                                                     
  $select = "SELECT materialId,materialName                                    
	             FROM material
	            ORDER BY materialName";                                                   
	$selectResult = mysql_query($select);                                       
	while($row = mysql_fetch_array($selectResult))                              
	{                                                                           
	  $catmaterialArray['materialId'][$m]   = $row['materialId'];                    
	  $catmaterialArray['materialName'][$m] = $row['materialName'];                      
	  $m++;                                                                     
	}                    
	//---------------------Quencing Media---------------------------
	$n = 0;                                                                     
	$selectQ = "SELECT qMId,qMName                                    
	              FROM quenchingmedia
	             ORDER BY qMName";                                                   
	$selectQResult = mysql_query($selectQ);                                       
	while($qrow = mysql_fetch_array($selectQResult))                              
	{                                                                           
	  $qmArray['qMId'][$n]   = $qrow['qMId'];                    
	  $qmArray['qMName'][$n] = $qrow['qMName'];                      
	  $n++;                                                                     
	}                    
	
	include("./bottom.php");
  $smarty->assign("catmaterialArray",$catmaterialArray);
	$smarty->assign("qmArray",$qmArray);
	$smarty->assign("logDataArray",$logDataArray);
	$smarty->assign("incDtlArray",$incDtlArray);
	$smarty->display("processMonitoring.tpl");
}
?>